﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using QLGT.Models;
using System.Web.Script.Serialization;
using System.Data.OleDb;
using System.Data;
using System.Data.Entity.Validation;
using System.Text;

namespace QLGT.Controllers
{
    public class QLTaiKhoanController : Controller
    {
        QLGTContext db = new QLGTContext();

        // GET: QLTaiKhoan

        #region QLChucNang
        public JsonResult LayChucNang()
        {
            return Json(db.tbl_ChucNang.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult ChucNang()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult HienThiChucNang([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.tbl_ChucNang.ToDataSourceResult(request));
        }

        //[ValidateAntiForgeryToken]
        public ActionResult TaoChucNang([DataSourceRequest] DataSourceRequest request, tbl_ChucNang data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.tbl_ChucNang.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult SuaChucNang([DataSourceRequest] DataSourceRequest request, tbl_ChucNang data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult XoaChucNang([DataSourceRequest] DataSourceRequest request, tbl_ChucNang data)
        {
            if (data != null)
            {
                tbl_ChucNang itemdata = db.tbl_ChucNang.First(item => item.MaCN == data.MaCN);
                db.tbl_ChucNang.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion

        #region QLQuyen
        public JsonResult LayQuyen()
        {
            return Json(db.tbl_Quyen.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Quyen()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult HienThiQuyen([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.tbl_Quyen.ToDataSourceResult(request));
        }

        //[ValidateAntiForgeryToken]
        public ActionResult TaoQuyen([DataSourceRequest] DataSourceRequest request, tbl_Quyen data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.tbl_Quyen.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult SuaQuyen([DataSourceRequest] DataSourceRequest request, tbl_Quyen data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult XoaQuyen([DataSourceRequest] DataSourceRequest request, tbl_Quyen data)
        {
            if (data != null)
            {
                tbl_Quyen itemdata = db.tbl_Quyen.First(item => item.MaQuyen == data.MaQuyen);
                db.tbl_Quyen.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
    }
}